using System;
using System.Collections.Generic;
using System.Text;
using Oracle.DataAccess.Client;
using System.Data;
using System.Windows.Forms;

namespace GEMASTIK_NET
{
    class MyData
    {
        private OracleConnection conn;
        private string OracleServer = "Data Source=(DESCRIPTION="
             + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))"
             + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)));"
             + "User Id=rechian;Password=rechian;";

        public void Open()
        {
            try
            {
                conn = new OracleConnection(OracleServer);
                conn.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void Close()
        {
            conn.Close();
            conn.Dispose();
        }

        public DataTable ExecuteDataTable(string sql)
        {
            DataTable dt = new DataTable();
            OracleDataAdapter da = new OracleDataAdapter(sql, conn);
            da.Fill(dt);
            return dt;
        }

        public DataSet ExecuteDataSet(string sql)
        {
            DataSet ds = new DataSet();
            OracleDataAdapter da = new OracleDataAdapter(sql, conn);
            da.Fill(ds, "result");
            return ds;
        }

        public OracleDataReader ExecuteReader(string sql)
        {
            OracleDataReader reader;
            OracleCommand cmd = new OracleCommand(sql, conn);
            reader = cmd.ExecuteReader();
            return reader;
        }

        public int ExecuteNonQuery(string sql)
        {
            int affected;
            try
            {
                OracleTransaction mytransaction = conn.BeginTransaction();
                OracleCommand cmd = conn.CreateCommand();
                cmd.CommandText = sql;
                affected = cmd.ExecuteNonQuery();
                mytransaction.Commit();
                return affected;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                return 0;
            }

        }
    }
}
